package com.ep.ratingbar.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ep.ratingbar.R;

/**
 * Created by Administrator on 2017/7/11.
 */

public class RatintBar extends View {
    private Bitmap mStartNomal;
    private Bitmap mStartFoucs;
    private int mStartGrade=5;

    private Paint mPaint;
    private int mCurGrade=0;

    public RatintBar(Context context) {
        this(context,null);
    }

    public RatintBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RatintBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.RatintBar);
        int nomalId = typedArray.getResourceId(R.styleable.RatintBar_startNomal, 0);
        if (nomalId==0){
            throw new RuntimeException("请设置startNomal属性");
        }
        mStartNomal= BitmapFactory.decodeResource(getResources(),nomalId);

        int foucsId=typedArray.getResourceId(R.styleable.RatintBar_startFoucs,0);
        if (foucsId==0){
            throw new RuntimeException("请设置startFoucs属性");
        }
        mStartFoucs= BitmapFactory.decodeResource(getResources(),foucsId);

        mStartGrade=typedArray.getInt(R.styleable.RatintBar_startGrade,mStartGrade);
        typedArray.recycle();

        mPaint=new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height=mStartNomal.getHeight();
        int width=mStartGrade*mStartNomal.getWidth();
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < mStartGrade; i++) {
            int left=i*mStartNomal.getWidth();
            if (mCurGrade>i){
                canvas.drawBitmap(mStartFoucs,left,0,null);
            }else {
                canvas.drawBitmap(mStartNomal,left,0,null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                int curGrade=(int)(x/mStartNomal.getWidth()+1);
                if (curGrade<0){
                    curGrade=0;
                }
                if (curGrade>mStartGrade){
                    curGrade=mStartGrade;
                }
                if (curGrade==mCurGrade){
                    return true;
                }

                mCurGrade=curGrade;
                invalidate();
                break;
        }
        return true;
    }
}
